﻿using System.Collections.Specialized;
using System.Data;
using System.IO;
using System.Text;
using AutoUnitTestEngine.Helpers;

namespace AutoUnitTestEngine.ValObjs
{
    /// <summary>
    /// 从内容对象解析出来的执行Sql对象的封装
    /// </summary>
    public class ValExecSql
    {
        public ValExecSql()
        {
            Timeout = 60000;
            SqlsForCancel = new StringCollection();
            SqlsForModify = new StringCollection();
            SqlsForInsert = new StringCollection();
            IsOutputTextLog = false;
        }


        public StringCollection SqlsForInsert { get; set; }
        public StringCollection SqlsForModify { get; set; }
        public StringCollection SqlsForCancel { get; set; }


        /// <summary>
        /// 执行超时时间（单位：毫秒，默认为60000，一分钟）
        /// </summary>
        public int Timeout { get; set; }

        /// <summary>
        /// 执行过程中是否输出TextLog
        /// </summary>
        public bool IsOutputTextLog { get; set; }

        public StringCollection UnionTheSqls()
        {
            var reVal = new StringCollection();
            reVal.Add("--Insert                 Begin>>>>>>>>>>>\r\n");
            foreach (string str in SqlsForInsert)
                reVal.Add(str);
            reVal.Add("\r\n--Insert                   End<<<<<<<<<<<\r\n\r\n\r\n");
            reVal.Add("--ModiFy                 Begin>>>>>>>>>>>\r\n");
            foreach (string str in SqlsForModify)
                reVal.Add(str);
            reVal.Add("\r\n--Modify                   End<<<<<<<<<<<\r\n\r\n\r\n");
            reVal.Add("--Cancel                 Begin>>>>>>>>>>>\r\n");
            foreach (string str in SqlsForCancel)
                reVal.Add(str);
            reVal.Add("\r\n--Cancel                   End<<<<<<<<<<<");
            return reVal;
        }

        public string GetRunSql()
        {
            var sbSql = new StringBuilder();
            var sc = UnionTheSqls();
            if(sc!=null && sc.Count>0)
            {
                foreach (string strSql in sc)
                    sbSql.Append(strSql + "\r\n");
            }
            return sbSql.ToString();
        }

        public void OutputTextToFile(string filePath)
        {
            if (File.Exists(filePath)) File.Delete(filePath);
            var sw = new StreamWriter(filePath);
            try
            {
                var context = GetRunSql();
                sw.Write(context);
            }
            finally
            {
                sw.Close();
                sw.Dispose();
            }
        }

        internal void CreateInsertSql(DataRow row, string dbTableName,StringCollection scNotExitingColNames)
        {
            string sql = SqlBuilder.CreateInsertSql(row, dbTableName,scNotExitingColNames);
            SqlsForInsert.Add(sql + ";");
        }

        internal void CreateInsertSqlByInsertColNames(DataRow row, string dbTableName, StringCollection scInsertColNames)
        {
            string sql = SqlBuilder.CreateInsertSqlByInsertColNames(row, dbTableName, scInsertColNames);
            SqlsForInsert.Add(sql + ";");
        }

        internal void CreateUpdateSql(DataRow row, string dbTableName, StringCollection updateColNames,
                                    StringCollection keyColNames)
        {
            string sql = SqlBuilder.CreateUpdateSql(row, dbTableName, updateColNames, keyColNames);
            SqlsForModify.Add(sql + ";");
        }
    }
}